﻿using GenericHtml;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using WLFramework.Validation;
using WLFramework.Web;
using WLFramework.DataAccess;
using WLFramework.Data.Linq;
using WLFramework.Json;

namespace SampleSite.admin.ajax.table
{
    /// <summary>
    /// addColumn 的摘要说明
    /// </summary>
    public class addColumn : AjaxHttpHandler
    {
        public class BaseData
        {
            [Required]
            public string Key { get; set; }

            [Required]
            public string Name { get; set; }

            [Required]
            public GenericHtml.ColumnType Type { get; set; }

            [Required]
            public int Control { get; set; }

            [Required]
            public int TableId { get; set; }

            public string[] Options { get; set; }
        }

        public class NumberData
        {
            [Required]
            [MinValue(0)]
            [MaxValue(4)]
            public int Precision { get; set; }

            public decimal? MinNumber { get; set; }

            public decimal? MaxNumber { get; set; }
        }

        public class StringData
        {
            [Required]
            [MinValue(0)]
            [MaxValue(4000)]
            public int MinLength { get; set; }

            [Required]
            [MinValue(0)]
            [MaxValue(4000)]
            public int MaxLength { get; set; }
        }

        public override void OnProcessRequest(HttpContext context)
        {
            BaseData baseData;
            if (context.GetModel<BaseData>(out baseData).Count > 0)
                throw new ArgumentException("参数错误");

            baseData.Key = baseData.Key.ToLower();
            if (!NameHelper.CheckColumnName(baseData.Key))
                throw new ArgumentException("参数错误");

            NumberData number = null;
            StringData str = null;

            if (baseData.Type == GenericHtml.ColumnType.DECIMAL)
                if (context.GetModel<NumberData>(out number).Count > 0)
                    throw new ArgumentException("参数错误");

            if (baseData.Type == GenericHtml.ColumnType.STRING)
                if (context.GetModel<StringData>(out str).Count > 0)
                    throw new ArgumentException("参数错误");

            if (str != null && str.MinLength > str.MaxLength)
            {
                int tmp = str.MinLength;
                str.MinLength = str.MaxLength;
                str.MaxLength = tmp;
            }

            using (CommandHelper db = new CommandHelper(CommandMethod.Write))
            {
                db.Context.BeginTransaction();

                var table = db.Context.dy_DynamicTables.Where(k => k.Id == baseData.TableId)
                              .FirstOrDefault();
                if (table == null)
                    throw new ArgumentException("参数错误");
                if (table.State != 0)
                    throw new ArgumentException("已经发布后的表不能添加新字段");                

                var column = db.Context.dy_DynamicColumns.Where(
                    k => k.TableId == baseData.TableId &&
                         k.ColumnName == baseData.Key).Count();
                if (column > 0)
                    throw new ArgumentException("关键字重复");

                bool required = baseData.Options.Contains("required");
                bool edit = baseData.Options.Contains("edit");

                db.Context.dy_DynamicColumns
                  .Insert(() => new dy_DynamicColumns
                  {
                      TableId = baseData.TableId,
                      ControlId = baseData.Control,
                      TypeId = (int)baseData.Type,
                      Required = required,
                      CanEdit = edit,
                      ColumnName = baseData.Key,
                      DisplayName = baseData.Name,
                      MinLength = str != null ? new Nullable<int>(str.MinLength) : null,
                      MaxLength = str != null ? new Nullable<int>(str.MaxLength) : null,
                      MinNumber = number != null ? number.MinNumber : null,
                      MaxNumber = number != null ? number.MaxNumber : null,
                      Precision = number == null ? 0 : number.Precision,
                  });

                db.Context.CommitTransaction();
            }

            context.Response.Write(JSON.Instance.ToJSON(JSONResult.Success));
        }
    }
}